package com.starblink.android.common.view.post

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.common.R
import com.starblink.rocketreserver.FetchResourceConfigQuery
import com.youth.banner.adapter.BannerAdapter

class DiscoverBannerImageAdapter(
    datas: List<FetchResourceConfigQuery.FetchResourceConfig?>,
//    val list: ArrayList<Any>,
    val context: Context,
    val pageTrackTag: String,
    val call: (jumpUrl: String?) -> Unit,
) :
    BannerAdapter<FetchResourceConfigQuery.FetchResourceConfig, DiscoverBannerImageAdapter.BannerViewHolder>(
        datas
    ) {

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder? {
        val view: View =
            LayoutInflater.from(context).inflate(R.layout.item_image_banner_post, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindView(
        holder: BannerViewHolder,
        data: FetchResourceConfigQuery.FetchResourceConfig,
        position: Int,
        size: Int
    ) {
        ImageUtils.loadImage(context, data.resourceF.mainPic, holder.img)
        val jumpUrl: String? = data.resourceF.jumpUrl
        holder.img.setOnClickListener {
            call.invoke(jumpUrl)
        }
//        Log.e("DiscoverBanner", "AA======$position=====${data.resourceF.id}------${list.size}")
        holder.img.trackData(
            page = SpmPageDef.RecommendPage,
            element = SpmElementDef.RecommendBannerCardV60,
            params = mapOf(
                "index" to position.toString()
            ),
//            businessSuffixID = businessSuffixId(
//                "FetchResourceConfig",
//                data.resourceF.id.toString()
//            )
            businessSuffixID = businessSuffixId(data.resourceF)
        )
    }

    class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var img: ImageView

        init {
            img = view.findViewById(R.id.iv_post)
        }
    }
}